/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package inet.controller;

import inet.dao.EmployeeDAO;
import inet.dao.PositionDAO;
import inet.entity.Branch;
import inet.entity.Employee;
import inet.entity.Position;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author MiSoKo
 */
@ManagedBean
@SessionScoped
public class EmployeeController {

    private Employee emp = null;
    private Employee empshow = null;
    private Branch branch = null;
    private Position posi = null;
    private Position posishow = null;
    private PositionDAO posiDAO = null;
    private EmployeeDAO empDAO = null;
    private List<Employee> lstEmp = null;

    /**
     * Creates a new instance of EmployeeController
     */
    public EmployeeController() {
        emp = new Employee();
        branch = new Branch();
        empshow = new Employee();
        posi = new Position();
        posishow = new Position();
        posiDAO = new PositionDAO();
        empDAO = new EmployeeDAO();
        lstEmp = new LinkedList<Employee>();
    }

    public Position getPosishow() {
        return posishow;
    }

    public Branch getBranch() {
        return branch;
    }

    public void setBranch(Branch branch) {
        this.branch = branch;
    }

    public void setPosishow(Position posishow) {
        this.posishow = posishow;
    }

    public Employee getEmpshow() {
        return empshow;
    }

    public void setEmpshow(Employee empshow) {
        this.empshow = empshow;
    }

    public Employee getEmp() {
        return emp;
    }

    public void setEmp(Employee emp) {
        this.emp = emp;
    }

    public Position getPosi() {
        return posi;
    }

    public void setPosi(Position posi) {
        this.posi = posi;
    }

    public PositionDAO getPosiDAO() {
        return posiDAO;
    }

    public void setPosiDAO(PositionDAO posiDAO) {
        this.posiDAO = posiDAO;
    }

    public EmployeeDAO getEmpDAO() {
        return empDAO;
    }

    public void setEmpDAO(EmployeeDAO empDAO) {
        this.empDAO = empDAO;
    }

    public List<Employee> getLstEmp() {
        return lstEmp;
    }

    public void setLstEmp(List<Employee> lstEmp) {
        this.lstEmp = lstEmp;
    }

    public void addEmployess() {
        empDAO = new EmployeeDAO();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (empDAO.checkEmpCode(emp.getEmp_code()) == 0) {
            if (empDAO.checkAcount(emp.getAcc_name()) == 0) {
                if (empDAO.addEmpCommit(emp, posi)) {
                    facesContext.addMessage("frmAdd", new FacesMessage("Thêm mới thất bại."));
                } else {
                    facesContext.addMessage("frmAdd", new FacesMessage("Thêm mới thành công."));
                }

            } else {
                facesContext.addMessage("frmAdd", new FacesMessage("Tài khoản này đã được sử dụng."));
            }
        } else {
            facesContext.addMessage("frmAdd", new FacesMessage("Mã nhân viên trùng nhau."));
        }
    }

    //show nhan viên
    public List<Employee> getShowEmployee() {
        empDAO = new EmployeeDAO();
        return empDAO.getAllEmployee(empshow.getLast_name(), branch.getBranch_id(), posishow.getDep_id());
    }
    //show nhan viên
    public List<Employee> getShowEMPTK() {
        empDAO = new EmployeeDAO();
        return empDAO.getAllEMP();
    }

    public void search() {
        getShowEmployee();
    }

    //Ẩn nhân viên
    public void hideEmp(BigDecimal empid) {
        empDAO = new EmployeeDAO();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (empDAO.updateStatusEmp(empid)) {
            facesContext.addMessage("an", new FacesMessage("Ẩn nhân viên thành công."));
        } else {
            facesContext.addMessage("an", new FacesMessage("Bạn không thể ẩn nhân viên này. " + empid));
        }
    }

//    public static void main(String[] a) {
//        EmployeeController c = new EmployeeController();
//        Employee e = new Employee();
//        if (c.getShowEmployee() == null) {
//            System.out.print("Bạn chưa có dữ liệu");
//        } else {
//            c.search();
//            System.out.print(e.getFirst_name()+e.getLast_name());
//        }
//    }
}
